import type { Customer, CustomerApproval } from "@/service/api/types/user";
import type { Response } from "@/service/api/types/request";
import type { MockAdapterInstance } from "../index";

import _ from "lodash";

// 生成随机用户数据
const generateCustomers = (n: number): Customer[] => {
    const usernames = ["Alice", "Bob", "Charlie", "David", "Eve"];
    const companyNames = [
        "ABC Corp",
        "XYZ Ltd",
        "123 Industries",
        "Tech Solutions",
    ];

    return _.times(n, () => {
        return {
            id: _.uniqueId(),
            account: `acc_${_.random(10000, 99999)}`,
            username: _.sample(usernames) as string, // 从usernames数组中随机选择一个值
            password: _.random(10000000, 99999999).toString(),
            frozen: _.random(0, 1) === 0,
            role: "customer",
            createdDate: Date.now(),
            phone: `1${_.random(1000000000, 9999999999)}`,
            companyName: _.sample(companyNames) as string,
        };
    });
};

// 生成随机客户注册申请数据
const generateCustomerApprovals = (n: number): CustomerApproval[] => {
    const oneMonthAgo = new Date();
    oneMonthAgo.setMonth(new Date().getMonth() - 1);
    return _.times(n, () => {
        const customer = generateCustomers(1)[0];
        return {
            id: _.uniqueId(),
            customerInfo: customer,
            status: _.random(0, 1) as 0 | 1,
            comments: "",
            finished: _.random(0, 10) <= 7 ? true : false,
            createdTime: _.random(oneMonthAgo.getTime(), Date.now()),
            processedTime: Date.now(),
        };
    });
};

const customers = generateCustomers(100);

const customerApprovals = generateCustomerApprovals(_.random(15, 30));

const unfinishedApprovals = customerApprovals.filter(
    (approval: CustomerApproval) => !approval.finished
);

const finishedApprovals = customerApprovals.filter(
    (approval: CustomerApproval) => approval.finished
);

export default function customerMock(mock: MockAdapterInstance) {
    // 获取客户列表（分页）
    mock.onPost("/user/admin/customer/list").reply((config) => {
        const response: Response<Customer[]> = {} as Response<Customer[]>;

        const { page, size } = JSON.parse(config.data);

        const start = (page - 1) * size;
        const end = start + size;

        response.code = 1;
        response.data = customers.slice(start, end);

        return [200, response];
    });

    // 获取还未处理的客户注册申请（获取全部）
    mock.onGet("/user/admin/approval/unfinished").reply(() => {
        const response: Response<CustomerApproval[]> = {} as Response<
            CustomerApproval[]
        >;

        response.code = 1;
        response.data = unfinishedApprovals;

        return [200, response];
    });

    // 获取已经处理的客户注册申请历史（分页）
    mock.onPost("/user/admin/approval/finished").reply((config) => {
        const response: Response<CustomerApproval[]> = {} as Response<
            CustomerApproval[]
        >;

        const { page, size } = JSON.parse(config.data);

        if ((page - 1) * size > finishedApprovals.length) {
            response.code = 1;
            response.data = [];
            return [200, response];
        }

        const start = (page - 1) * size;
        const end = start + size;

        response.code = 1;
        response.data = finishedApprovals.slice(start, end);

        return [200, response];
    });
}
